<?php

include_once 'settingManager.php';
function generateSettingForm($action)
{
    $setting = settingManager::getSettings($_SESSION['userID']);

    if ($setting->getCalendar() == "1")
        $calendar = "checked";

    if ($setting->getGrades() == "1")
        $grades = "checked";
    
    if ($setting->getLinks() == "1")
        $links = "checked";
    
    if ($setting->getFacebook() == "1")
        $facebook = "checked";
    
    if ($setting->getTwitter() == "1")
        $twitter = "checked";
    
    if ($setting->getTumblr() == "1")
        $tumblr = "checked";
    
    if ($setting->getLinkedIn() == "1")
        $linkedIn = "checked";
    
    if ($setting->getEmailBool() == "1")
        $emailBool = "checked";
    
    if ($setting->getTextBool() == "1")
        $textBool = "checked";
    
    $form = "<form action=$action method=\"POST\">
        Please select the modules and alert settings you would like to use:<br>
        <table>
        <tr><td>Calendar</td><td><input type=\"checkbox\" name=\"calendar\" value=\"1\" $calendar></td></tr>
        <tr><td>Grades</td><td><input type=\"checkbox\" name=\"grades\" value=\"1\" $grades></td></tr>
        <tr><td>Links</td><td><input type=\"checkbox\" name=\"links\" value=\"1\" $links></td></tr>
        <tr><td>Facebook</td><td><input type=\"checkbox\" name=\"facebook\" value=\"1\" $facebook></td></tr>
        <tr><td>Twitter</td><td><input type=\"checkbox\" name=\"twitter\" value=\"1\" $twitter></td></tr>
        <tr><td>Tumblr</td><td><input type=\"checkbox\" name=\"tumblr\" value=\"1\" $tumblr></td></tr>
        <tr><td>LinkedIn</td><td><input type=\"checkbox\" name=\"linkedIn\" value=\"1\" $linkedIn></td></tr>
        <tr><td>Email Alerts</td><td><input type=\"checkbox\" name=\"emailBool\" value=\"1\" $emailBool></td></tr>
        <tr><td>Text Alerts</td><td><input type=\"checkbox\" name=\"textBool\" value=\"1\" $textBool></td></tr>
        </table>
        <input type=\"submit\" value=\"Submit\"></form>
        ";

    return $form;
}

if ($edit == "settings")
{
    if ($submit == "1")
    {
        $setting = new Setting();
        $setting->setUserID($_SESSION['userID']);

        if ($_POST['calendar'] == "1")
            $setting->setCalendar("1");
        else
            $setting->setCalendar("0");

        if ($_POST['grades'] == "1")
            $setting->setGrades("1");
        else
            $setting->setGrades("0");

        if ($_POST['links'] == "1")
            $setting->setLinks("1");
        else
            $setting->setLinks("0");

        if ($_POST['facebook'] == "1")
            $setting->setFacebook("1");
        else
            $setting->setFacebook("0");

        if ($_POST['twitter'] == "1")
            $setting->setTwitter("1");
        else
            $setting->setTwitter("0");

        if ($_POST['tumblr'] == "1")
            $setting->setTumblr("1");
        else
            $setting->setTumblr("0");

        if ($_POST['linkedIn'] == "1")
            $setting->setLinkedIn("1");
        else
            $setting->setLinkedIn("0");

        if ($_POST['emailBool'] == "1")
            $setting->setEmailBool("1");
        else
            $setting->setEmailBool("0");

        if ($_POST['textBool'] == "1")
            $setting->setTextBool("1");
        else
            $setting->setTextBool("0");
        
        settingManager::editSetting($setting);
    } else
    {
        $action = "index.php?edit=settings&submit=1";
        echo generateSettingForm($action);
    }
}

?>
